package com.t.blog.utils;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;
import com.t.blog.dto.SysUserDto;
import org.t.pro.vo.SysUser;

/**
 * 〈一句话功能简述〉<br>
 * 〈密码工具类〉
 *
 * @author a0000
 * @create 2023/10/30 23:58
 * @since 1.0.0
 */
@Component
public class PassUtils {


    @Autowired
    private PasswordEncoder BCryptPasswordEncoder;

    public SysUser PassWithSalt(SysUser user){
        String salt = getSalt(user.getSalt());
        String prefix = StrUtil.sub(salt, 0, 3);
        String suffix = StrUtil.sub(salt,3,-1);
        String password = user.getPassword();
        if (user.getSalt() == null ){
            password = BCryptPasswordEncoder.encode(prefix + password + suffix);
        }else{
            password = prefix + password + suffix;
        }
        user.setPassword(password);
        user.setSalt(salt);
        return user;
    }


    private String getSalt(String salt){
        if (salt == null) {
            return  RandomUtil.randomString(6);
        }
        return salt;
    }

//    public static void main(String[] args) {
//        String rightToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJyb2xlcyI6IixhZG1pbiIsIm5pY2tuYW1lIjoidG9tYW5kamVycnkiLCJleHBpcmVfdGltZSI6MTcwMDE1MjkwNDU5OCwidXNlcm5hbWUiOiJ0b21tdSJ9.Rd3EbBYqTbx7NX5sdrNhExy_rx7lAbIeD6veKFg4jKk";
//
//        final JWT jwt = JWTUtil.parseToken(rightToken);
//
//        jwt.getHeader(JWTHeader.TYPE);
//        System.out.println(jwt.getPayload("roles"));
//    }

}
